from django.contrib import admin
from .models import TaskBlock


@admin.register(TaskBlock)
class TaskBlockAdmin(admin.ModelAdmin):
    """
    任务块管理界面
    """
    list_display = [
        'task_name', 'duration', 'get_required_devices', 
        'status', 'created_at', 'updated_at'
    ]
    list_filter = ['status', 'created_at']
    search_fields = ['task_name', 'required_devices__device_name']
    readonly_fields = ['created_at', 'updated_at']
    # list_per_page = 25  # 默认分页设置
    
    def get_required_devices(self, obj):
        return ', '.join([device.device_name for device in obj.required_devices.all()])
    get_required_devices.short_description = '所需设备'
    
    fieldsets = (
        ('基本信息', {
            'fields': ('task_name', 'duration', 'required_devices', 'status')
        }),
        ('配置信息', {
            'fields': ('daily_device_config',)
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        })
    )
